package de.carcophan.jimdb.service.json;

import java.util.Iterator;
import java.util.List;

import de.carcophan.jimdb.gwt.shared.entites.Movie;

public class ToEntity {

   public ToEntity() {

   }

   public Movie toMovie(MovieJson movieJson) {
      Movie movie = new Movie();
      movie.setActors(listToString(movieJson.getActors()));
      movie.setAlsoKnownAs(listToString(movieJson.getAlso_known_as()));
      movie.setCountry(listToString(movieJson.getCountry()));
      movie.setDirectors(listToString(movieJson.getDirectors()));
      movie.setFilmingLocations(movieJson.getFilming_locations());
      movie.setGenres(listToString(movieJson.getGenres()));
      movie.setImdbId(movieJson.getImdb_id());
      movie.setImdbUrl(movieJson.getImdb_url());
      movie.setLanguage(listToString(movieJson.getLanguage()));
      movie.setPlotSimple(movieJson.getPlot_simple());
      movie.setPoster(movieJson.getPoster());
      movie.setRated(movieJson.getRated());
      movie.setRating(movieJson.getRating());
      movie.setRatingCount(movieJson.getRating_count());
      movie.setReleaseDate(movieJson.getRelease_date());
      movie.setRuntime(listToString(movieJson.getRuntime()));
      movie.setTitle(movieJson.getTitle());
      movie.setType(movieJson.getType());
      movie.setWriters(listToString(movieJson.getWriters()));
      movie.setYear(movieJson.getYear());
      return movie;
   }

   private String listToString(List<?> list) {
      StringBuilder stringBuilder = new StringBuilder();
      if (list != null) {
         for (Iterator<?> iterator = list.iterator(); iterator.hasNext();) {
            stringBuilder.append(iterator.next().toString());
            if (iterator.hasNext()) {
               stringBuilder.append(", ");
            }
         }
      }
      return stringBuilder.toString();
   }

}
